<template>
  <div class="notice-mgr" v-loading.fullscreen.lock="loading">
    <page-main :pagination="pagination" @search-data="searchData" @page-change="pageChange">
      <template slot="search">
        <search-form :search="search"></search-form>
      </template>
      <template slot="append">
        <el-button type="primary" size="medium" @click="addNotice">新增</el-button>
      </template>
      <template slot="table">
        <el-table fit stripe border highlight-current-row :data="noticeList" height="100%">
          <el-table-column prop="nitoceTitle" label="公告标题" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="公告图片" width="122px" header-align="center">
            <template slot-scope="scope" v-if="scope.row.noticeImgUrl.length>0">
              <el-image style="width:100px;height:100px;float:left" :src="scope.row.noticeImgUrl[0]" fit="scale-down"
                :preview-src-list="scope.row.noticeImgUrl">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="noticeTipUrl" label="活动详情地址" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="创建日期" width="140px" align="center">
            <template slot-scope="scope">
              <span>{{$moment(scope.row.createTime).format('YYYY-MM-DD HH:mm')}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="状态" width="80px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg(`log.noticeMgr.status.${scope.row.status}`)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="updateNotice(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </page-main>

    <el-dialog :title="dialogTitle[dialogType]" custom-class="sys-dialog" :close-on-click-modal="false" :visible.sync="openDialog" width="500px"
      top="10vh" @closed="dialogClosed">
      <div style="height:calc(80vh - 54px);overflow:auto">
        <notice-form v-if="openDialog" :notice="selectNotice" :type="dialogType" @save-success="saveSuccess"></notice-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import LogApi from 'services/logApi';

  import PageMain from 'commonComponents/pageMain';
  import SearchForm from './searchForm';
  import NoticeForm from './noticeForm';

  import Pagination from 'models/pagination';
  import NoticeSearch from 'models/log/noticeSearch';

  export default {
    data() {
      return {
        loading: false,
        search: new NoticeSearch(),
        pagination: new Pagination(),
        noticeList: [],
        openDialog: false,
        dialogType: '',
        selectNotice: null,
        dialogTitle: {
          add: "新增公告",
          update: "修改公告"
        }
      };
    },

    created() {
      this.setNoticeList(true);
    },

    methods: {
      setNoticeList(isSearch = false) {
        let search = this.search;
        if (!isSearch) {
          search.resetOldData();
        }

        let pagination = this.pagination;
        this.loading = true;
        LogApi.getNoticeList(search, pagination).then(noticeList => {
          if (isSearch) {
            search.setOldData();
          }
          this.noticeList = noticeList;
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },

      searchData() {
        this.pagination.currentPage = 1;
        this.setNoticeList(true);
      },

      pageChange() {
        this.setNoticeList();
      },

      addNotice() {
        this.dialogType = 'add';
        this.openDialog = true;
      },

      updateNotice(notice) {
        this.dialogType = 'update';
        this.selectNotice = notice;
        this.openDialog = true;
      },

      saveSuccess() {
        this.openDialog = false;
        this.setNoticeList();
      },

      dialogClosed() {
        this.dialogType = '';
        this.selectNotice = null;
      }
    },

    components: {
      PageMain,
      SearchForm,
      NoticeForm
    }
  };

</script>

<style lang="scss" scoped>
  .notice-mgr {
    height: 100%;
  }

</style>
